package io.renren.modules.sys.service.impl;

import io.renren.common.utils.Constant;
import io.renren.modules.sys.dao.MySysMenuDao;
import io.renren.modules.sys.dao.MySysUserDao;
import io.renren.modules.sys.dao.MySysUserTokenDao;
import io.renren.modules.sys.entity.MySysMenuEntity;
import io.renren.modules.sys.entity.MySysUserEntity;
import io.renren.modules.sys.service.MyShiroService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("MyShiroService")
public class MyShiroServiceImpl implements MyShiroService {

    @Autowired
    private MySysMenuDao mySysMenuDao;

    @Autowired
    private MySysUserDao mySysUserDao;

    @Autowired
    private MySysUserTokenDao mySysUserTokenDao;

    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> permissionList;
        if (userId == Constant.SUPER_ADMIN) {
            List<MySysMenuEntity> menuList = mySysMenuDao.selectList(null);
            permissionList = new ArrayList<>(menuList.size());
            for (MySysMenuEntity menu : menuList) {
                permissionList.add(menu.getPerms());
            }
        }else{
            permissionList = mySysUserDao.queryAllPerms(userId);
        }
        Set<String> permsSet = new HashSet<>();
        System.out.println("====================" + permissionList);

        for (String perms :permissionList) {
            if(StringUtils.isBlank(perms)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        System.out.println("===============" + permsSet);

        return permsSet;
    }
}
